﻿using System;
using System.Linq;

namespace YArchitech.Plumbing
{
	public class GHCalcMethods
	{
		public static int GetGJIndex(int diameter)
		{
			int num = 0;
			while (num < 11 && ConstantOfGHCalc.GJB[num] != diameter)
			{
				num++;
			}
			return num;
		}

		public static double GetXPAPWJB(int inerdiameter)
		{
			int num = 0;
			while (num < ConstantOfGHCalc.XPAPWJB.Count<double>() && ConstantOfGHCalc.XPAPGJB[num] != inerdiameter)
			{
				num++;
			}
			if (num >= 2)
			{
				num = 2;
			}
			return ConstantOfGHCalc.XPAPWJB[num];
		}

		public static double GetJSNJ(int diameter)
		{
			int num = 0;
			while (num < 11 && ConstantOfGHCalc.GJB[num] != diameter)
			{
				num++;
			}
			return ConstantOfGHCalc.NJB[num];
		}

		public static int GetGCZJ(double inerDia)
		{
			int num = 0;
			while (num < 11 && inerDia > ConstantOfGHCalc.NJB[num])
			{
				num++;
			}
			return ConstantOfGHCalc.GJB[num];
		}

		public static double GetLocalCoeff(ValveString valveName, int diameter)
		{
			double result = 0.0;
			int gjindex = GHCalcMethods.GetGJIndex(diameter);
			switch (valveName)
			{
			case ValveString.闸阀:
				result = ConstantOfGHCalc.ZF_JZXS[gjindex];
				break;
			case ValveString.截止阀:
				result = ConstantOfGHCalc.JZF_JZXS[gjindex];
				break;
			case ValveString.斜杆截止阀:
				result = ConstantOfGHCalc.XJZF_JZXS[gjindex];
				break;
			case ValveString.弯头:
				result = ConstantOfGHCalc.WT_JZXS[gjindex];
				break;
			case ValveString.乙字弯:
				result = ConstantOfGHCalc.YZW_JZXS[gjindex];
				break;
			case ValveString.扩弯:
				result = ConstantOfGHCalc.KW_JZXS[gjindex];
				break;
			case ValveString.方形补偿器:
				result = 2.0;
				break;
			case ValveString.套管补偿器:
				result = 0.5;
				break;
			case ValveString.直流三通:
				result = 1.0;
				break;
			case ValveString.旁流三通:
				result = 1.5;
				break;
			case ValveString.分流三通:
				result = 3.0;
				break;
			case ValveString.合流三通:
				result = 3.0;
				break;
			case ValveString.直流四通:
				result = 2.0;
				break;
			case ValveString.分合流四通:
				result = 3.0;
				break;
			}
			return result;
		}

		public static ValveString GetValveStringByName(string valveName)
		{
			return (ValveString)Enum.Parse(typeof(ValveString), valveName);
		}

		public static double GetSpeed(double flow, int DN, double density)
		{
			double result = 0.0;
			if (Math.Abs(flow) < 1E-06 || DN == 0)
			{
				return result;
			}
			int num = 0;
			while (num < 11 && ConstantOfGHCalc.GJB[num] != DN)
			{
				num++;
			}
			double num2 = ConstantOfGHCalc.NJB[num] * Math.Pow(10.0, -3.0);
			return flow / (2827.4333882308138 * num2 * num2 * density);
		}

		public static double GetSpeed(double density, double interDia, double flow)
		{
			double result = 0.0;
			if (Math.Abs(flow) < 1E-06 || interDia == 0.0)
			{
				return result;
			}
			double num = interDia * Math.Pow(10.0, -3.0);
			return flow / (2827.4333882308138 * num * num * density);
		}

		public static double GetFrictionCoeffic(double speed, int DN, double K, double KViscosity)
		{
			double num = 0.0;
			if (Math.Abs(speed) < 1E-06 || DN == 0)
			{
				return num;
			}
			int num2 = 0;
			while (num2 < 11 && ConstantOfGHCalc.GJB[num2] != DN)
			{
				num2++;
			}
			double num3 = ConstantOfGHCalc.NJB[num2];
			double num4 = speed * num3 * Math.Pow(10.0, -3.0) / (KViscosity * Math.Pow(10.0, -6.0));
			double num5 = K / num3;
			if (num4 <= 2320.0)
			{
				num = 64.0 / num4;
			}
			else
			{
				num = 0.1;
				int num6 = 0;
				double num7 = 0.1;
				while (num7 > Math.Pow(10.0, -6.0) && num6 <= 10)
				{
					double num8 = 2.51 / (num4 * Math.Sqrt(num)) + num5 / 3.72;
					num8 = -2.0 * Math.Log10(num8);
					double num9 = 1.0 / (num8 * num8);
					num7 = Math.Abs(num9 - num);
					num = num9;
					num6++;
				}
			}
			return num;
		}

		public static double GetRm(double flow, int DN, double λ, double density)
		{
			double result = 0.0;
			if (Math.Abs(flow) < 1E-06 || DN == 0)
			{
				return result;
			}
			int num = 0;
			while (num < 11 && ConstantOfGHCalc.GJB[num] != DN)
			{
				num++;
			}
			double x = ConstantOfGHCalc.NJB[num] * Math.Pow(10.0, -3.0);
			return 6.25 * Math.Pow(10.0, -8.0) * λ * flow * flow / (density * Math.Pow(x, 5.0));
		}

		public static double GetRm(double flow, double interDia, double λ, double density)
		{
			double result = 0.0;
			if (Math.Abs(flow) < 1E-06 || interDia == 0.0)
			{
				return result;
			}
			interDia *= Math.Pow(10.0, -3.0);
			return 6.25 * Math.Pow(10.0, -8.0) * λ * flow * flow / (density * Math.Pow(interDia, 5.0));
		}

		public static int GetDN(double flow, double Rm, double waterKViscosity, double waterDensity, double RoughNess)
		{
			int result = 20;
			double num = 10000.0;
			for (int i = 0; i < 11; i++)
			{
				int num2 = ConstantOfGHCalc.GJB[i];
				double frictionCoeffic = GHCalcMethods.GetFrictionCoeffic(GHCalcMethods.GetSpeed(flow, num2, waterDensity), num2, RoughNess, waterKViscosity);
				double num3 = Math.Abs(GHCalcMethods.GetRm(flow, num2, frictionCoeffic, waterDensity) - Rm);
				if (num3 <= num)
				{
					result = num2;
					num = num3;
				}
			}
			return result;
		}

		public static double GetXPAPV(double flow, double DN, double density)
		{
			double num = flow / 3600.0 / density;
			double num2 = DN / 1000.0;
			return num / (Math.PI * num2 * num2 / 4.0);
		}

		public static int GetXPAPDN(double flow, double density)
		{
			int num = 0;
			double num2 = 2.0;
			int num3;
			for (int i = 0; i < ConstantOfGHCalc.XPAPGJB.Count<int>(); i++)
			{
				num3 = ConstantOfGHCalc.XPAPGJB[i];
				double num4 = GHCalcMethods.GetXPAPV(flow, (double)num3, density) - 0.25;
				if (num4 >= 0.0 && num4 < num2)
				{
					num2 = num4;
					num = i;
				}
			}
			num3 = ConstantOfGHCalc.XPAPGJB[num];
			if (num2 == 2.0)
			{
				num3 = ConstantOfGHCalc.XPAPGJB[0];
			}
			return num3;
		}

		public static double GetXPAPR(double flow, double DN, double density, double kViscosity)
		{
			double num = flow / 3600.0 / density;
			double num2 = DN / 1000.0;
			double num3 = num / (Math.PI * num2 * num2 / 4.0);
			double x = num3 * num2 / (kViscosity * Math.Pow(10.0, -6.0));
			return 0.25 / Math.Pow(x, 0.226) * density * num3 * num3 / (2.0 * num2);
		}

		public static double GetWaterDensity(double temperature)
		{
			double[] array = new double[]
			{
				992.24,
				991.86,
				991.47,
				991.07,
				990.66,
				990.25,
				989.82,
				989.4,
				988.69,
				988.52,
				988.07,
				987.62,
				987.15,
				986.69,
				986.21,
				985.73,
				985.25,
				984.75,
				984.25,
				983.75,
				983.24,
				982.72,
				982.2,
				981.67,
				981.13,
				980.59,
				980.05,
				979.5,
				978.94,
				978.38,
				977.81,
				977.23,
				976.66,
				976.07,
				975.48,
				974.84,
				974.29,
				973.68,
				973.07,
				972.45,
				971.83,
				971.21,
				970.57,
				969.94,
				969.3,
				968.65,
				968.0,
				967.34,
				966.68,
				966.01,
				965.34,
				964.67,
				963.99,
				963.3,
				962.61,
				961.92,
				961.22,
				960.51,
				959.81,
				959.09,
				958.38
			};
			double[] array2 = new double[]
			{
				999.8,
				999.73,
				998.23,
				995.67,
				992.24
			};
			if (temperature < 0.0)
			{
				return 999.8;
			}
			int num = Convert.ToInt32(temperature + 0.5);
			if (num > 100)
			{
				num = 100;
			}
			if (num >= 40)
			{
				return array[num - 40];
			}
			double num3;
			double num4;
			if (num < 10)
			{
				int num2;
				if (num < 4)
				{
					num2 = 0;
					num3 = array2[0];
					num4 = 1000.0;
				}
				else
				{
					num2 = 4;
					num3 = 1000.0;
					num4 = array2[1];
				}
				return num3 + (temperature - (double)num2) / 10.0 * (num4 - num3);
			}
			int num5 = num / 10;
			num3 = array2[num5];
			num4 = array2[num5 + 1];
			return num3 + (temperature - (double)(num5 * 10)) / 10.0 * (num4 - num3);
		}

		public static double GetKinematicViscosity(double temperature)
		{
			int[] array = new int[]
			{
				0,
				5,
				10,
				15,
				20,
				30,
				40,
				60,
				80
			};
			double[] array2 = new double[]
			{
				1.792,
				1.52,
				1.307,
				1.139,
				1.004,
				0.801,
				0.658,
				0.475,
				0.365
			};
			int num = Convert.ToInt32(temperature + 0.5);
			if (num >= 80)
			{
				return array2[8];
			}
			double result = array2[0];
			for (int i = 0; i < 9; i++)
			{
				if (num < array[i])
				{
					double num2 = (double)(array[i] - array[i - 1]);
					double num3 = array2[i] - array2[i - 1];
					result = array2[i - 1] + (double)(num - array[i - 1]) * num3 / num2;
					break;
				}
			}
			return result;
		}

		public static double GetFlowByLoad(double load, double temperSupply, double temperReturn)
		{
			return 0.86 * load / GHCalcMethods.GetDiffTemperature(temperSupply, temperReturn);
		}

		public static double GetDiffTemperature(double temperSupply, double temperReturn)
		{
			double result = 0.0;
			if (temperSupply > temperReturn)
			{
				result = temperSupply - temperReturn;
			}
			return result;
		}

		private const int GJIndexMax = 11;

		private const double ERROR6 = 1E-06;
	}
}
